package com.woniuxy.controller;

import com.woniuxy.domain.TreeNode;
import com.woniuxy.pojo.SysPermission;
import com.woniuxy.pojo.SysUser;
import com.woniuxy.service.PermissionService;
import com.woniuxy.util.TreeNodeBuilder;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MenuController {
    @Autowired
    private PermissionService permissionService;
    @RequestMapping(value = "/loadIndexLeftMenu.html")
    @ResponseBody
    public List<TreeNode> loadIndexLeftMenu(){
        Subject subject= SecurityUtils.getSubject();
        Session session=subject.getSession();
        SysUser user=(SysUser) session.getAttribute("SESSION_USER");
        //通过用户名查找用户信息
        List<SysPermission> permissionList=this.permissionService.getMenusByUserId(user.getId());

        //List<SysPermission> permissionList=permissionService.getAllMenus();
        List<TreeNode> treeNodes=new ArrayList<>();
        for (SysPermission permission:permissionList) {
            int id=permission.getId();
            int pid=permission.getPid();
            String title=permission.getName();
            String icon=permission.getIcon();
            String href=permission.getHref();
            boolean spread=permission.getOpen()==1?true:false;
            TreeNode treeNode=new TreeNode(id,pid,title,icon,href,spread);
            treeNodes.add(treeNode);
        }
        //构造层级关系
        List<TreeNode> treeNodes1= TreeNodeBuilder.build(treeNodes,1);
        return treeNodes1;
    }
}
